// pages/service-center-detail/service-center-detail.js
const app = getApp()
const common = require('../../utils/common.js')

Page({
  data: {
    detail: {
      title: '',
      subtitle: '',
      icon: '',
      process: [],
      advantages: [],
      faqs: [],
      contactTip: '',
      contactInfo: {
        address: '',
        phone: '400-XXX-XXXX'
      }
    }
  },

  onLoad(options) {
    let detail = common.getStorage('currentService')
    
    if (detail) {
      // 确保有完整的数据结构
      if (!detail.contactInfo) {
        detail.contactInfo = {
          phone: '400-XXX-XXXX'
        }
      }
      if (!detail.contactInfo.phone) {
        detail.contactInfo.phone = '400-XXX-XXXX'
      }
      
      this.setData({ detail })
      
      // 设置导航栏标题
      wx.setNavigationBarTitle({
        title: detail.title
      })
    } else {
      // 如果没有缓存数据，使用默认数据
      common.showToast('暂无数据', 'none')
      setTimeout(() => {
        wx.navigateBack()
      }, 1500)
    }
  },

  // 拨打电话
  onCallPhone() {
    const phone = this.data.detail.contactInfo?.phone || '400-XXX-XXXX'
    wx.makePhoneCall({
      phoneNumber: phone,
      fail: () => {
        common.showToast('拨打失败', 'error')
      }
    })
  },

  // 分享
  onShareAppMessage() {
    return {
      title: this.data.detail.title + ' - 壹加壹农业',
      path: '/pages/service-center-detail/service-center-detail'
    }
  }
})
